package ebcs.scan.controller.systemCtr;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import ebcs.database.bean.BsGroupInfor;
import ebcs.database.bean.BsNeedInfor;
import ebcs.database.bean.BsUserInfor;
import ebcs.database.bean.BsUserNeedRel;
import ebcs.database.bean.RpUserTaskNeedBean;
import ebcs.database.bean.SysFunction;
import ebcs.database.service.BsGroupInforServiceImp;
import ebcs.database.service.SystemServiceImp;
import ebcs.database.service.impl.BsUserInforServiceImpl;
import ebcs.database.service.impl.BsUserNeedServiceImpl;
import ebcs.scan.common.consts.KLinkUsSysConsts;
import jplat.core.session.JSession;
import jplat.core.trans.JAppBaseService;
import jplat.core.trans.JAppContext;
import jplat.core.trans.JAppContextFactory;
import jplat.error.exception.JSystemException;
import jplat.error.exception.JTransException;
import jplat.tools.string.JRandomUtil;
 

/***
 * 系统管理
 * @author chenzhuo
 * @comment
 */
@Controller
public class SystemManagerCtrl extends JAppBaseService
{
	private Logger logger = Logger.getLogger(SystemManagerCtrl.class);
	protected JAppContextFactory serviceFactory = new JAppContextFactory();
	//数据库库服务.
	@Autowired 
	public SystemServiceImp systemService ; 
	
	@Autowired
    public BsGroupInforServiceImp groupService;
  
	/**
	 * 拉去用户所属权限
	 */
	@RequestMapping("/page/need/selectFunction.do")
	public void selectFunction( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
		BsUserInfor user = context.getUserInfo();
		List funcList =  systemService.selectFunction(user);
 	 	Map rsModel = new HashMap();
 	 	rsModel.put("funcList", funcList);
  		context.buildAppResponse(rsModel);
	}
	/**
	 * 保存用户信息至session
	 * @param request
	 * @param response
	 * @param user
	 * @throws JSystemException 
	 */
	public String saveSessionInfor(HttpServletRequest request, HttpServletResponse response, BsUserInfor user) throws JSystemException {
		JSession session;
        JAppContext context = serviceFactory.buildAppContextNoUser(request, response);        //buildAppContextWithUser 自带客户信息.
        session = context.createSession(true);
        session.setUserInfo(user);
        
        request.getSession().setAttribute("loginUserName", user.getUserName());
        request.getSession().setAttribute("loginUser", user);
        System.out.println("-------------------"+user.getPosition_type()+"-------------------------");
        System.out.println("-------------------"+user.getProTeamNo()+"-------------------------");
        if(user.getPosition_type()!=null&&user.getProTeamNo()!=null){
	        if(user.getPosition_type().equals("69")&&!user.getProTeamNo().equals("103")){
	         List<BsGroupInfor> bsGroupInfors = groupService.selectGroupByUserRouid(user.getRouid());
	         request.getSession().setAttribute("loginGroupInfo", bsGroupInfors);
	        }//else if(user.getPosition_type().equals("70")){
	        else{
	        	List<BsGroupInfor> bsGroupInfors = groupService.selectProGroupNoByUserNo(user.getUserNo());
	        	request.getSession().setAttribute("loginGroupInfo", bsGroupInfors);
	        }
        }else{
        	 List<BsGroupInfor> bsGroupInfors = groupService.selectGroupByUserRouid(user.getRouid());
	         request.getSession().setAttribute("loginGroupInfo", bsGroupInfors);
        }
		return session.getSessionID();
	}
}
